//
//  HotListView.m
//  何五路
//
//  Created by _ADY on 15/7/8.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "HotListView.h"

@implementation HotListView
@synthesize ShopsLArray,shopsId,delegate;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        vFrame = frame;

        myTableView = [[UITableView alloc] initWithFrame:frame];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.backgroundColor = [UIColor clearColor];
        [self addSubview:myTableView];
        
        UIView *v = [[UIView alloc] initWithFrame:CGRectZero];//清除多余分割线
        [myTableView setTableFooterView:v];
        
        
        if (ShopsLArray == nil) {
            ShopsLArray = [[NSMutableArray alloc] init];
           [self performSelector:@selector(afterShops) withObject:self afterDelay:.5];
        }
        else
            [myTableView reloadData];
        
    }
    return self;
}

-(void)afterShops
{
    NSMutableDictionary *postDic = [[NSMutableDictionary alloc] init];
    [postDic setObject:shopsId forKey:@"shopId"];
    [postDic setObject:@"1" forKey:@"levelsToLoad"];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary *ret = [SendService GetShopCategoriesById:postDic];
        dispatch_async(dispatch_get_main_queue(), ^{
            ShopsLArray =  [ret objectForKey:@"Entity"];
            NSLog(@"店铺分类接口 %@",ret);
            [myTableView reloadData];
            
        });
    });
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return ShopsLArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"search";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        aTableView.showsVerticalScrollIndicator = NO;
    }
//    cell.backgroundColor = [UIColor colorWithWhite:.95 alpha:1];
    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@",[[ShopsLArray objectAtIndex:indexPath.row] objectForKey:@"Name"]];
    cell.textLabel.textAlignment = 1;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    [self.delegate HotListID:[NSString stringWithFormat:@"%@",[[ShopsLArray objectAtIndex:indexPath.row] objectForKey:@"CategoryId"]]];
}


@end
